﻿Imports BUS
Imports DTO

Public Class frmCacLoaiCa

    Private thaotac As String = "sua"

    Private Sub EnableText(ByVal b As Boolean)
        txtmaca.Enabled = b
        txttenca.Enabled = b
        dtgiovao.Enabled = b
        dtgiora.Enabled = b
        dtnghitu.Enabled = b
        dtnghiden.Enabled = b
    End Sub

    Private Sub XoaText()
        txtmaca.Text = ""
        txttenca.Text = ""
        dtgiovao.Value = "#" & Date.Now.Date & " " & "00:00:00" & "#"
        dtgiora.Value = "#" & Date.Now.Date & " " & "00:00:00" & "#"
        dtnghitu.Value = "#" & Date.Now.Date & " " & "00:00:00" & "#"
        dtnghiden.Value = "#" & Date.Now.Date & " " & "00:00:00" & "#"
    End Sub

    Private Sub GanText(ByVal index As Integer)
        txtmaca.Text = dgvcacloaica.Rows(index).Cells(0).Value
        txttenca.Text = dgvcacloaica.Rows(index).Cells(1).Value
        dtgiovao.Value = "#" & Date.Now.Date & " " & dgvcacloaica.Rows(index).Cells(2).Value.ToString() & "#"
        dtnghitu.Value = "#" & Date.Now.Date & " " & dgvcacloaica.Rows(index).Cells(3).Value.ToString() & "#"
        dtnghiden.Value = "#" & Date.Now.Date & " " & dgvcacloaica.Rows(index).Cells(4).Value.ToString() & "#"
        dtgiora.Value = "#" & Date.Now.Date & " " & dgvcacloaica.Rows(index).Cells(5).Value.ToString() & "#"

        thaotac = "sua"
    End Sub

    Private Sub frmCacLoaiCa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dt As New DataTable
        dt = CacLoaiCaBus.LayDanhSach()
        dgvcacloaica.DataSource = dt

        EnableText(False)
        Try
            GanText(0)
        Catch ex As Exception
            MessageBox.Show("loi")
        End Try

    End Sub

    Private Sub dgvcacloaica_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvcacloaica.CellClick
        Try
            GanText(e.RowIndex)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub ThêmToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThêmToolStripMenuItem.Click
        thaotac = "them"
        XoaText()
        EnableText(True)
    End Sub

    Private Sub XóaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XóaToolStripMenuItem.Click
        If txtmaca.Text = "" Then
            Return
        End If

        Dim res As MsgBoxResult = MsgBox("Ban co muon xoa", MsgBoxStyle.YesNo, "Thong bao")
        Select Case res
            Case MsgBoxResult.Yes
                CacLoaiCaBus.XoaCa(txtmaca.Text)

                For i As Integer = 0 To dgvcacloaica.Rows.Count - 1 Step 1
                    If txtmaca.Text = dgvcacloaica.Rows(i).Cells(1).Value.ToString() Then
                        dgvcacloaica.Rows.RemoveAt(i)
                        Exit For
                    End If
                Next

                MessageBox.Show("Da xoa ca")
            Case MsgBoxResult.No
                Return
        End Select

    End Sub

    Private Sub SửaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SửaToolStripMenuItem.Click
        thaotac = "sua"
        EnableText(True)
        txtmaca.Enabled = False
    End Sub

    Private Sub LưuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LưuToolStripMenuItem.Click
        Dim ca As New CacLoaiCaDto
        ca.MaCa = txtmaca.Text
        ca.TenCa = txttenca.Text
        ca.GioVao = dtgiovao.Value.Hour.ToString() & ":" & dtgiovao.Value.Minute.ToString() & ":" & dtgiovao.Value.Millisecond.ToString()
        ca.GioRa = dtgiora.Value.Hour.ToString() & ":" & dtgiora.Value.Minute.ToString() & ":" & dtgiora.Value.Millisecond.ToString()
        ca.NghiTu = dtnghitu.Value.Hour.ToString() & ":" & dtnghitu.Value.Minute.ToString() & ":" & dtnghitu.Value.Millisecond.ToString()
        ca.NghiDen = dtnghiden.Value.Hour.ToString() & ":" & dtnghiden.Value.Minute.ToString() & ":" & dtnghiden.Value.Millisecond.ToString()

        If thaotac = "them" Then
            If txtmaca.Text = "" Or txttenca.Text = "" Then
                MessageBox.Show("Vui long nhap du thong tin")
                Return
            End If

            CacLoaiCaBus.ThemCa(ca)
            txtmaca.Enabled = False
            thaotac = "sua"
            MessageBox.Show("Da them ca")
        Else
            CacLoaiCaBus.SuaCa(ca)
            MessageBox.Show("Da sua ca")
        End If

    End Sub
End Class